package c2.group.business.teampvp;


import cate.common.table.d.GDFight;
import cate.game.play.part.PlaySide;
import cp.solution.util.collection.ListKit;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class TeamPvpFightTask {
    /** 1单身狗 2组队 */
    public byte type;
    @NoteField("匹配结束时间")
    public long matchEndTime;
    @NoteField("布阵结束时间")
    public long positionEndTime;
    @NoteField("刷新次数")
    public int refreshTimes;
    @NoteField("a方阵容")
    public List<TeamPvpPlayer> ateam;
    @NoteField("b方阵容")
    public List<TeamPvpPlayer> bteam;
    @NoteField("服务器是否战斗结束")
    public boolean over;
    @NoteField("是否挑战中")
    private volatile boolean fighting;
    @NoteField("a方赢的次数")
    private int awins;
    @NoteField("b方赢的次数")
    private int bwins;
    @NoteField("真实玩家uid")
    public List<String> memberUids;
    @NoteField("挑战者uid，播剧本")
    public String challengerUid;
    @NoteField("队长uid")
    public String leaderUid;
    @NoteField("挑战前积分")
    public long preScore;

    public TeamPvpFightTask() {

    }

    public TeamPvpFightTask(String challengerUid, String leaderUid, long preScore, byte type, List<TeamPvpPlayer> ateam, List<TeamPvpPlayer> bteam) {
        this.challengerUid = challengerUid;
        this.leaderUid = leaderUid;
        this.preScore = preScore;
        this.ateam = ListKit.cloneList(ateam, TeamPvpPlayer::copy);
        this.bteam = ListKit.cloneList(bteam, TeamPvpPlayer::copy);
        this.matchEndTime = System.currentTimeMillis() + 60000;
        this.positionEndTime = this.matchEndTime + 60000;
        this.type = type;
        this.memberUids = type == 1 ? Collections.singletonList(ateam.get(0).ps.snapshot.uid)
                : ateam.subList(0, 2).stream().map(e -> e.ps.snapshot.uid).collect(Collectors.toList());
    }

    public long ateamAverageScore() {
        if (CollectionUtils.isEmpty(ateam)) {
            return 1000;
        }
        return ateam.stream().mapToLong(e -> e.score).sum() / 3;
    }

    public long bteamAverageScore() {
        if (CollectionUtils.isEmpty(bteam)) {
            return 1000;
        }
        return bteam.stream().mapToLong(e -> e.score).sum() / 3;
    }

    public void winner(byte team) {
        if (team == GDFight.Team.A) {
            awins ++;
        } else {
            bwins ++;
        }
    }

    public boolean awin() {
        return awins >= 2;
    }

    public boolean isAllFightsOver() {
        return awins >= 2 || bwins >= 2;
    }

    public boolean isMatchOver() {
        return !fighting && !over && System.currentTimeMillis() >= positionEndTime;
    }

    public boolean isFightOver() {
        return !fighting && over;
    }

    public void beginToFight() {
        fighting = true;
    }

    public void endToFight() {
        this.fighting = false;
        this.over = true;
    }

    public boolean isFighting() {
        return fighting;
    }

    public void refresh(List<TeamPvpPlayer> ateam, List<TeamPvpPlayer> bteam) {
        refreshTimes++;
        this.ateam = ListKit.cloneList(ateam, TeamPvpPlayer::copy);
        this.bteam = ListKit.cloneList(bteam, TeamPvpPlayer::copy);
    }

    public boolean updatePos(List<String> newUids) {
        List<TeamPvpPlayer> newPlayers = new ArrayList<>();
        for (String newUid : newUids) {
            for (TeamPvpPlayer player : ateam) {
                if (StringUtils.equals(player.ps.snapshot.uid, newUid)) {
                    newPlayers.add(player);
                }
            }
        }
        if (newPlayers.size() != ateam.size()) {
            return false;
        }
        ateam = newPlayers;
        return true;
    }

    public void savePosition(PlaySide ps) {
        for (TeamPvpPlayer player : ateam) {
            if (StringUtils.equals(player.ps.snapshot.uid, ps.snapshot.uid)) {
                player.ps = ps.copy();
                player.power = ps.getTotalPower();
            }
        }
    }

    public void aheadToPosition() {
        matchEndTime = System.currentTimeMillis();
        positionEndTime = System.currentTimeMillis() + 60000;
    }
}
